/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tweetbotserver;

import tweetbotserver.robot.comms.BotMessage;
import tweetbotserver.server.comms.TwitterCommunicationsListener;
import tweetbotserver.robot.comms.RobotCommunicationsListener;
import tweetbotserver.robot.comms.RobotCommunicationsManager;
import tweetbotserver.server.comms.TwitterCommunicationsManager;
import tweetbotserver.server.message.MessageProcessor;
import tweetbotserver.server.message.TwitterMessageProcessor;

/**
 *
 * @author Mike
 */
public class TweetBotController implements RobotCommunicationsListener, TwitterCommunicationsListener {

    private RobotCommunicationsManager robotCommsManager = null;
    private TwitterCommunicationsManager twitterCommsManager = null;
    private MessageProcessor messageProcessor = null;

    public TweetBotController(RobotCommunicationsManager robotComms, TwitterCommunicationsManager twitterComms) {
        robotCommsManager = robotComms;
        twitterCommsManager = twitterComms;
        robotCommsManager.addCommunicationsListener(this);
        twitterCommsManager.addTwitterCommunicationsListener(this);
        messageProcessor = new TwitterMessageProcessor();
        System.out.println("Created TweetBot Controller");

    }

    @Override
    public void handleBotMessage(BotMessage message) {

        System.out.println("Handling Bot message:" + message);

        String messageData = messageProcessor.parseBotMessage(message);

        twitterCommsManager.tweet(messageData);

    }

    @Override
    public void handleTwitterMessage(String message) {
        System.out.println("Handling twitter message:" + message);

        BotMessage botMessage = messageProcessor.parseData(message);

        try {
            robotCommsManager.sendMessage(botMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
